package com.hzlx.finishedGoods.controller;

import java.util.List;
import java.io.IOException;
import com.hzlx.ResultDto;
import com.hzlx.finishedGoods.domain.CheckListRequest;

import com.hzlx.MesFinishedGoodsInventoryDto;
import com.hzlx.ResultDto;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hzlx.finishedGoods.domain.MesFinishedGoodsInventory;
import com.hzlx.finishedGoods.service.IMesFinishedGoodsInventoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 入库信息管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-30
 */
@RestController
@Slf4j
@RequestMapping("/inventory")
public class MesFinishedGoodsInventoryController extends BaseController
{
    @Autowired
    private IMesFinishedGoodsInventoryService mesFinishedGoodsInventoryService;
    /**
     * 查询入库信息管理列表
     */
    @PostMapping("/goods/list")
    public TableDataInfo list(MesFinishedGoodsInventory mesFinishedGoodsInventory, HttpServletRequest request)
    {
        startPage();
        List<MesFinishedGoodsInventory> list = mesFinishedGoodsInventoryService.selectMesFinishedGoodsInventoryList(mesFinishedGoodsInventory);
        return getDataTable(list);
    }

    //批量查询
    @RequiresPermissions("material-service:inventory:goods:checkList")
    @PostMapping("/goods/checkList")
    public ResultDto<List<MesFinishedGoodsInventory>> checkList(@RequestBody CheckListRequest request){
        // 1. 添加日志记录请求参数
        log.info("检查请求参数 orderId:{}, orderNums:{}",
                request.getOrderId(), request.getOrderNums());

        // 2. 检查数据库查询性能
        long start = System.currentTimeMillis();

        List<MesFinishedGoodsInventory>  list= mesFinishedGoodsInventoryService.selectByOrderIdAndOrderNums(request.getOrderId(), request.getOrderNums());
        log.info("查询耗时: {}ms", System.currentTimeMillis() - start);
            return ResultDto.<List<MesFinishedGoodsInventory>>builder().code(200).data(list).build();
    }

    /**
     * 导出入库信息管理列表
     */
    @RequiresPermissions("material-service:inventory:goods:export")
    @Log(title = "入库信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/goods/export")
    public void export(HttpServletResponse response, MesFinishedGoodsInventory mesFinishedGoodsInventory)
    {
        List<MesFinishedGoodsInventory> list = mesFinishedGoodsInventoryService.selectMesFinishedGoodsInventoryList(mesFinishedGoodsInventory);
        ExcelUtil<MesFinishedGoodsInventory> util = new ExcelUtil<MesFinishedGoodsInventory>(MesFinishedGoodsInventory.class);
        util.exportExcel(response, list, "入库信息管理数据");
    }

    /**
     * 获取入库信息管理详细信息
     */
    @RequiresPermissions("material-service:inventory:goods:query")
    @GetMapping(value = "/goods/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mesFinishedGoodsInventoryService.selectMesFinishedGoodsInventoryById(id));
    }

    /**
     * 新增入库信息管理
     */
    @RequiresPermissions("material-service:inventory:goods:add")
    @Log(title = "入库信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/goods")
    public AjaxResult add(@RequestBody MesFinishedGoodsInventory mesFinishedGoodsInventory)
    {
        return toAjax(mesFinishedGoodsInventoryService.insertMesFinishedGoodsInventory(mesFinishedGoodsInventory));
    }

    /**
     * 修改入库信息管理
     */
    @RequiresPermissions("material-service:inventory:goods:edit")
    @Log(title = "入库信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/goods")
    public AjaxResult edit(@RequestBody MesFinishedGoodsInventory mesFinishedGoodsInventory)
    {
        return toAjax(mesFinishedGoodsInventoryService.updateMesFinishedGoodsInventory(mesFinishedGoodsInventory));
    }

    /**
     * 删除入库信息管理
     */
    @RequiresPermissions("material-service:inventory:goods:remove")
    @Log(title = "入库信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/goods/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesFinishedGoodsInventoryService.deleteMesFinishedGoodsInventoryByIds(ids));
    }

    @RequiresPermissions("material-service:inventory:goods:edit")
    @Log(title = "入库信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/package/{id}")
    public ResultDto<String> pack(@PathVariable Long id){
        return mesFinishedGoodsInventoryService.pack(id);
    }

    @RequiresPermissions("material-service:inventory:goods:list")
    @GetMapping("/listByOrderId")
    public List<MesFinishedGoodsInventory> getListByOrderId(@Param("orderId") String orderId){
        return mesFinishedGoodsInventoryService.selectListByOrderId(orderId);
    }


    @RequiresPermissions("material-service:inventory:goods:add")
    @Log(title = "入库信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add/badGoods")
    public ResultDto<String> addBadGoods(@RequestBody MesFinishedGoodsInventoryDto mesFinishedGoodsInventoryDto)
    {
        mesFinishedGoodsInventoryService.insertMesFinishedGoodsInventoryByBadGoods(mesFinishedGoodsInventoryDto);
        return ResultDto.<String>builder().code(200).build();
    }
}
